<template>
  <div class="activity-detail">
    <el-header class="header">
      <div class="header-content">
        <div class="logo">
          <h2>🏃‍♂️ 健身活动预约系统</h2>
        </div>
        <div class="nav">
          <el-button @click="$router.push('/user/home')" type="primary">首页</el-button>
          <el-button @click="$router.push('/user/activities')">活动列表</el-button>
          <el-button @click="$router.push('/user/bookings')">我的预约</el-button>
          <el-button @click="logout" type="danger">退出登录</el-button>
        </div>
      </div>
    </el-header>
    <el-main class="main-content">
      <el-card v-if="activity" class="detail-card">
        <div class="activity-header">
          <h1>{{ activity.title }}</h1>
          <el-tag :type="activity.status === 1 ? 'success' : 'danger'">
            {{ activity.status === 1 ? '正常' : '已取消' }}
          </el-tag>
        </div>
        
        <div class="activity-content">
          <div class="activity-image">
            <img :src="getActivityImage(activity.title)" :alt="activity.title">
          </div>
          
          <div class="activity-info">
            <p class="description">{{ activity.description }}</p>
            <div class="info-item">
              <span class="label">📍 地点：</span>
              <span class="value">{{ activity.location }}</span>
            </div>
            <div class="info-item">
              <span class="label">🕐 开始时间：</span>
              <span class="value">{{ formatTime(activity.startTime) }}</span>
            </div>
            <div class="info-item">
              <span class="label">🕐 结束时间：</span>
              <span class="value">{{ formatTime(activity.endTime) }}</span>
            </div>
            <div class="info-item">
              <span class="label">💰 价格：</span>
              <span class="value price">¥{{ activity.price }}</span>
            </div>
            <div class="info-item">
              <span class="label">👥 参与人数：</span>
              <span class="value">{{ activity.currentParticipants }}/{{ activity.maxParticipants }}</span>
            </div>
            <div class="info-item" v-if="activity.remark">
              <span class="label">📝 备注：</span>
              <span class="value">{{ activity.remark }}</span>
            </div>
          </div>
        </div>
        
        <div class="action-buttons">
          <el-button @click="$router.push('/user/activities')" size="large">返回列表</el-button>
          <el-button 
            type="primary" 
            size="large" 
            @click="bookActivity"
            :disabled="activity.status === 0 || activity.currentParticipants >= activity.maxParticipants"
          >
            {{ activity.currentParticipants >= activity.maxParticipants ? '已满员' : '立即预约' }}
          </el-button>
        </div>
      </el-card>
      
      <el-empty v-else description="活动不存在" />
    </el-main>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { activityApi } from '@/api'
import dayjs from 'dayjs'

const router = useRouter()
const route = useRoute()

const activity = ref(null)

const loadActivityDetail = async () => {
  try {
    const id = route.params.id
    const res = await activityApi.getActivityDetail(id)
    activity.value = res.data.activity
  } catch (error) {
    console.error('加载活动详情失败:', error)
    ElMessage.error('加载活动详情失败')
  }
}

const bookActivity = () => {
  router.push(`/user/booking/create?activityId=${activity.value.id}`)
}

const logout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('userInfo')
  ElMessage.success('退出登录成功')
  router.push('/login')
}

const formatTime = (time) => {
  return dayjs(time).format('YYYY-MM-DD HH:mm')
}

const getActivityImage = (title) => {
  const imageMap = {
    '瑜伽': '/images/yoga.jpg',
    '力量': '/images/strength.jpg',
    '有氧': '/images/cardio.jpg',
    '游泳': '/images/swimming.jpg',
    '拳击': '/images/boxing.jpg'
  }
  
  for (const [key, image] of Object.entries(imageMap)) {
    if (title.includes(key)) {
      return image
    }
  }
  return '/images/default.jpg'
}

onMounted(() => {
  loadActivityDetail()
})
</script>

<style scoped>
.activity-detail {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.header {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid #e0e0e0;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
}

.logo h2 {
  margin: 0;
  color: #333;
}

.nav .el-button {
  margin-left: 10px;
}

.main-content {
  padding: 20px;
}

.detail-card {
  max-width: 800px;
  margin: 0 auto;
  border-radius: 15px;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
}

.activity-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 2px solid #f0f0f0;
}

.activity-header h1 {
  margin: 0;
  color: #333;
  font-size: 2em;
}

.activity-content {
  display: flex;
  gap: 30px;
  margin-bottom: 30px;
}

.activity-image {
  flex: 0 0 300px;
}

.activity-image img {
  width: 100%;
  height: 200px;
  object-fit: cover;
  border-radius: 10px;
}

.activity-info {
  flex: 1;
}

.description {
  font-size: 16px;
  color: #666;
  line-height: 1.6;
  margin-bottom: 20px;
}

.info-item {
  display: flex;
  margin-bottom: 15px;
  align-items: center;
}

.label {
  font-weight: bold;
  color: #333;
  min-width: 120px;
}

.value {
  color: #666;
  flex: 1;
}

.value.price {
  color: #e74c3c;
  font-size: 18px;
  font-weight: bold;
}

.action-buttons {
  display: flex;
  justify-content: center;
  gap: 20px;
  padding-top: 20px;
  border-top: 2px solid #f0f0f0;
}
</style> 